package com.example.basicknowledge.SQL数据库;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.basicknowledge.R;

public class SQLActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>, AdapterView
        .OnItemClickListener {
    private static final int PET_LOADER = 0;
    PetCursorAdapter mCursorAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sql_activity);
        FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab_button);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SQLActivity.this,EditorActivity.class);
                startActivity(intent);
            }
        });

        ListView petListView = (ListView)findViewById(R.id.pet_list_view);
        View emptyView = findViewById(R.id.empty_view);
        petListView.setEmptyView(emptyView);

        mCursorAdapter = new PetCursorAdapter(this,null);
        petListView.setAdapter(mCursorAdapter);

        petListView.setOnItemClickListener(this);

        getSupportLoaderManager().initLoader(PET_LOADER,null,this);
    }

    private void insertData() {
        ContentValues values = new ContentValues();
//        values.put(PetContract.PetEntry._ID,9);
        values.put(PetContract.PetEntry.COLUMN_PET_BREED,"Terrier");
        values.put(PetContract.PetEntry.COLUMN_PET_GENDER, PetContract.PetEntry.GENDER_MALE);
        values.put(PetContract.PetEntry.COLUMN_PET_NAME,"Tole");
        values.put(PetContract.PetEntry.COLUMN_PET_WEIGHT,2);
        Uri newUri = getContentResolver().insert(PetContract.PetEntry.CONTENT_URI,values);
//        long newRowId = db.insert(PetContract.PetEntry.TABLE_NAME,null,values);
        Log.v("CatalogActivity","New row ID " + newUri.toString());

    }

    private void deleteAllData () {
        getContentResolver().delete(PetContract.PetEntry.CONTENT_URI,null,null);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu options from the res/menu/menu_catalog.xml file.
        // This adds menu items to the app bar.
        getMenuInflater().inflate(R.menu.menu_catalog, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // User clicked on a menu option in the app bar overflow menu
        switch (item.getItemId()) {
            // Respond to a click on the "Insert dummy data" menu option
            case R.id.action_insert_dummy_data:
                // Do nothing for now
                insertData();
//                displayDataBaseInfo();
                return true;
            // Respond to a click on the "Delete all entries" menu option
            case R.id.action_delete_all_entries:
                // Do nothing for now
                deleteAllData();
//                displayDataBaseInfo();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @NonNull
    @Override
    public Loader<Cursor> onCreateLoader(int i, @Nullable Bundle bundle) {
        String[] projection = {
                PetContract.PetEntry._ID,
                PetContract.PetEntry.COLUMN_PET_NAME,
                PetContract.PetEntry.COLUMN_PET_BREED,
        };
        return new CursorLoader(this, PetContract.PetEntry.CONTENT_URI,projection,null,null,null);
    }

    @Override
    public void onLoadFinished(@NonNull Loader<Cursor> loader, Cursor cursor) {
        mCursorAdapter.swapCursor(cursor);
    }

    @Override
    public void onLoaderReset(@NonNull Loader<Cursor> loader) {
        mCursorAdapter.swapCursor(null);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent = new Intent(SQLActivity.this,EditorActivity.class);
        Uri currentpetUri = ContentUris.withAppendedId(PetContract.PetEntry.CONTENT_URI,l);
        intent.setData(currentpetUri);
        startActivity(intent);
    }
}
